﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace YXYLCommon
{
    public partial class ListText : UserControl
    {
        private List<Item> Items = new List<Item>();

        private bool needReBind = true;

        public override string Text
        {
            get
            {
               return this.textBox1.Text;
            }
            set
            {
                this.needReBind = false;
                this.textBox1.Text = value;
                this.needReBind = true;
            }
        }
        public string Value
        {
            get
            {
                if(this.textBox1.Text.Trim()=="")
                {
                    return null;
                }
                return this.listBox1.SelectedValue == null ? null : this.listBox1.SelectedValue.ToString();
            }
            set
            {
                if (this.Items.Count > 0)
                {
                    List<Item> list = new List<Item>();
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        if (Items[i].Value == value)
                        {
                            list.Add(Items[i]);
                            break;
                        }
                    }
                    this.reBind(list);
                    if (this.listBox1.Items.Count != 0)
                    {
                        this.listBox1.SelectedIndices.Add(0);
                    }
                    this.Text = Convert.ToString(this.listBox1.SelectedItem);
                    this.Height = this.textBox1.Height;                    
                }
                else
                {
                    if (value != null)
                    {
                        List<Item> list = new List<Item>();
                        list.Add(new Item(value, value));
                        this.reBind(list);
                        if (this.listBox1.Items.Count != 0)
                        {
                            this.listBox1.SelectedIndices.Add(0);
                        }
                        this.Text = Convert.ToString(this.listBox1.SelectedItem);
                        
                    }
                }
            }
        }

        public bool HasItem
        {
            get { return (this.listBox1.Items.Count > 0); }
        }
        public ListText()
        {
            InitializeComponent();
            this.listBox1.DataSource = this.Items;
            this.listBox1.ValueMember = "Value";
            this.listBox1.DisplayMember = "Text";
        }

        public void AddItem(string text,string value)
        {
            this.Items.Add(new Item(text,value));
        }
        public void ReBind()
        {
            this.reBind(this.Items);
        }

        public event EventHandler MouseSingleClick;
        public event EventHandler ValueSelected;
        private void reBind(List<Item> list)
        {
            
            this.listBox1.DataSource = list;
            this.listBox1.ValueMember = "Value";
            this.listBox1.DisplayMember = "Text";
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            reLoad((TextBox)sender);
        }
        private void reLoad(TextBox txt)
        {
            if (this.needReBind)
            {
                this.Height = this.textBox1.Height + 1 + this.listBox1.Height;
                List<Item> list = new List<Item>();
                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (Items[i].Text.Contains((txt.Text)))
                    {
                        list.Add(Items[i]);
                    }
                }
                this.reBind(list);
                if (list.Count == 0)
                {
                    this.Height = this.textBox1.Height;
                }
            }
        }

       
      
        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.listBox1.Items.Count > this.listBox1.SelectedIndex + 1)
                {
                    this.listBox1.SelectedIndex = this.listBox1.SelectedIndex + 1;
                }
                else
                {                    
                    this.textBox1.Focus();
                    this.textBox1.SelectionStart = this.textBox1.Text.Length;
                    this.textBox1.SelectionLength = 0;
                    this.listBox1.SelectedIndices.Clear();
                }
            }
            else if (e.KeyCode == Keys.Space)
            {
                //将选中的值变更到文本上显示.此时要关闭text-change事件.
                this.needReBind = false;
                this.textBox1.Text = this.listBox1.SelectedItem.ToString();
                this.textBox1.Focus();
                //完成后重新启用text-change事件
                this.needReBind = true;
                this.textBox1.SelectionStart = this.textBox1.Text.Length;
                this.textBox1.SelectionLength = 0;
                this.Height = this.textBox1.Height;
                //调用取值完成事件
                if (this.ValueSelected != null)
                {
                    this.ValueSelected.Invoke(null, new EventArgs());
                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.listBox1.Items.Count > 0)
                {
                    this.listBox1.SelectedIndex = 0 ;
                    this.listBox1.Focus();
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                this.Parent.GetNextControl(this,true).Focus();
                this.Height = this.textBox1.Height;
            }
        }
        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Height = this.textBox1.Height;
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.MouseSingleClick != null&&e.Clicks==1)
            {
                this.MouseSingleClick.Invoke(((TextBox)sender).Parent, new EventArgs());
            }
        }

        private class Item 
        {
            public Item()
            { }
            public Item(string text,string value)
            {
                this.text = text;
                this.value = value;
            }
            private string text;
            public string Text
            {
                get
                {
                    return this.text;
                }
                set
                {
                    this.text = value;
                }
            }
            private string value;
            public string Value
            {
                get
                {
                    return this.value;
                }
                set
                {
                    this.value = value;
                }
            }
            public override string ToString()
            {
                return this.text;
            }
        }

        
    }
}
